/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Fatec
 */
public class HelloMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command calculoCommand;
    private Command backFromCalculoCommand;
    private Form formValores;
    private TextField textFieldKmTotal;
    private TextField textFieldKmPorLitro;
    private TextField textFieldValorLitro;
    private Form FormCalculo;
    private StringItem stringItemConsumoTotal;
    private StringItem stringItemValorTotal;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HelloMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
        FormCalculo = new Form("Valores Previstos", new Item[] { getStringItemConsumoTotal(), getStringItemValorTotal() });//GEN-BEGIN:|0-initialize|1|0-postInitialize
        FormCalculo.addCommand(getBackFromCalculoCommand());
        FormCalculo.setCommandListener(this);//GEN-END:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getFormValores());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == FormCalculo) {//GEN-BEGIN:|7-commandAction|1|26-preAction
            if (command == backFromCalculoCommand) {//GEN-END:|7-commandAction|1|26-preAction
                // write pre-action user code here
                switchDisplayable(null, getFormValores());//GEN-LINE:|7-commandAction|2|26-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|23-preAction
        } else if (displayable == formValores) {
            if (command == calculoCommand) {//GEN-END:|7-commandAction|3|23-preAction

                switchDisplayable(null, FormCalculo);//GEN-LINE:|7-commandAction|4|23-postAction
                double v1 = Double.parseDouble(textFieldKmTotal.getString());
                // variavel que representa a distancia a ser percorrida
                double v2 = Double.parseDouble(textFieldKmPorLitro.getString());
                // variavel que representa a quilometragem por litro do veiculo
                double v3 = Double.parseDouble(textFieldValorLitro.getString());
                // variavel que representa o valos estimado do litro de combustivel
                double total_litros = v1/v2;// calculando qtos litros serao utilizados
                double valor_total = total_litros*v3;// calculando o valor a ser gasto

               int n1 = (int) total_litros;
               int n2 = (int) ((total_litros - n1)*100);
                String saida1 = n1 + " , " + n2;

               int n3 = (int)valor_total;
               int n4 = (int)((valor_total - n3)*100);
                String saida2 = n3 + " , " + n4;

                 stringItemConsumoTotal.setText(saida1);
               //stringItemConsumoTotal.setText(Double.toString(total_litros));
             // stringItemValorTotal.setText(Double.toString(valor_total));// write post-action user code here
              stringItemValorTotal.setText(saida2);

            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|5|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|19-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
    //</editor-fold>//GEN-END:|7-commandAction|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: formValores ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of formValores component.
     * @return the initialized component instance
     */
    public Form getFormValores() {
        if (formValores == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            formValores = new Form("Preencha os Campos", new Item[] { getTextFieldKmTotal(), getTextFieldKmPorLitro(), getTextFieldValorLitro() });//GEN-BEGIN:|14-getter|1|14-postInit
            formValores.addCommand(getExitCommand());
            formValores.addCommand(getCalculoCommand());
            formValores.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return formValores;
    }
    //</editor-fold>//GEN-END:|14-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: calculoCommand ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of calculoCommand component.
     * @return the initialized component instance
     */
    public Command getCalculoCommand() {
        if (calculoCommand == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            calculoCommand = new Command("Calcular", Command.ITEM, 0);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return calculoCommand;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backFromCalculoCommand ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of backFromCalculoCommand component.
     * @return the initialized component instance
     */
    public Command getBackFromCalculoCommand() {
        if (backFromCalculoCommand == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            backFromCalculoCommand = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return backFromCalculoCommand;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textFieldKmTotal ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of textFieldKmTotal component.
     * @return the initialized component instance
     */
    public TextField getTextFieldKmTotal() {
        if (textFieldKmTotal == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            textFieldKmTotal = new TextField("Quantos km pretende viajar?", "", 32, TextField.ANY);//GEN-LINE:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return textFieldKmTotal;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textFieldKmPorLitro ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of textFieldKmPorLitro component.
     * @return the initialized component instance
     */
    public TextField getTextFieldKmPorLitro() {
        if (textFieldKmPorLitro == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            textFieldKmPorLitro = new TextField("Quantos km seu ve\u00EDculo faz com 1 litro?", "", 32, TextField.ANY);//GEN-LINE:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return textFieldKmPorLitro;
    }
    //</editor-fold>//GEN-END:|28-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textFieldValorLitro ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of textFieldValorLitro component.
     * @return the initialized component instance
     */
    public TextField getTextFieldValorLitro() {
        if (textFieldValorLitro == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            textFieldValorLitro = new TextField("Qual o pre\u00E7o m\u00E9dio do combust\u00EDvel utilizado?", "", 32, TextField.ANY);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return textFieldValorLitro;
    }
    //</editor-fold>//GEN-END:|29-getter|2|


    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemConsumoTotal ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of stringItemConsumoTotal component.
     * @return the initialized component instance
     */
    public StringItem getStringItemConsumoTotal() {
        if (stringItemConsumoTotal == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            stringItemConsumoTotal = new StringItem("Total de litros a serem consumidos", "");//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return stringItemConsumoTotal;
    }
    //</editor-fold>//GEN-END:|30-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemValorTotal ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initiliazed instance of stringItemValorTotal component.
     * @return the initialized component instance
     */
    public StringItem getStringItemValorTotal() {
        if (stringItemValorTotal == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            stringItemValorTotal = new StringItem("Valor total aproximado", "");//GEN-LINE:|31-getter|1|31-postInit
            // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return stringItemValorTotal;
    }
    //</editor-fold>//GEN-END:|31-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
